package com.vhall.component.service.rbac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.rbac.RbacAccessOpLogMapper;
import com.vhall.component.entity.rbac.AccessOpLogEntity;
import com.vhall.component.entity.rbac.vo.AccessOpLogListReqVO;
import com.vhall.component.entity.rbac.vo.AccessOpLogVO;
import com.vhall.component.service.rbac.RbacAccessOpLogService;
import com.vhall.component.service.rbac.constant.AccessConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dalong
 * @date 2021-05-10 19:34
 */
@Service
public class RbacAccessOpLogServiceImpl implements RbacAccessOpLogService {

    @Autowired
    private RbacAccessOpLogMapper accessOpLogMapper;


    @Override
    public List<AccessOpLogVO> list(AccessOpLogListReqVO accessGetLogReqVO) {
        int page = accessGetLogReqVO.getPage();
        LambdaQueryWrapper<AccessOpLogEntity> where = Wrappers.lambdaQuery();
        where.eq(AccessOpLogEntity::getOperator, accessGetLogReqVO.getAccountId())
                .orderByDesc(AccessOpLogEntity::getCreatedAt)
                .isNull(AccessOpLogEntity::getDeletedAt)
                .last("LIMIT " + (page - 1) * AccessConstant.PAGE_SIZE + "," + AccessConstant.PAGE_SIZE);
        List<AccessOpLogEntity> accessOpLogEntities = accessOpLogMapper.selectList(where);

        return accessOpLogEntities.stream().map(x -> {
            AccessOpLogVO accessOpLogVO = new AccessOpLogVO();
            BeanUtils.copyProperties(x, accessOpLogVO);
            return accessOpLogVO;
        }).collect(Collectors.toList());
    }
}
